{# your_app/templates/components/pagination.html #}
{% comment %}
可复用的分页组件
参数:
  - paginator: Paginator对象
  - page_obj: 当前页面对象
  - max_visible: 最大显示页码数（默认6）
  - theme: 主题样式（light/dark/custom）默认light
{% endcomment %}

{% load math_filters %}

{% with max_visible=max_visible|default:6 %}
    {% with theme=theme|default:'light' %}

        <div class="pagination-container {{ theme }}-theme">
            {% if paginator.count > 0 %}
                <div class="pagination-info text-center mb-2">
    <span class="text-muted">
      显示 {{ page_obj.start_index }}-{{ page_obj.end_index }} 条，
      共 {{ paginator.count|format_number }} 条
    </span>
                </div>
            {% endif %}

            <ul class="pagination justify-content-center">
                {# 首页按钮 #}
                {% if page_obj.has_previous %}
                    <li class="page-item">
                        <a class="page-link" href="?page=1" title="首页">
                            <span aria-hidden="true">&laquo;</span> 首页
                        </a>
                    </li>
                {% else %}
                    <li class="page-item disabled">
                        <span class="page-link"><span aria-hidden="true">&laquo;</span> 首页</span>
                    </li>
                {% endif %}

                {# 上一页按钮 #}
                {% if page_obj.has_previous %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ page_obj.previous_page_number }}" title="上一页">
                            <span aria-hidden="true">&lsaquo;</span> 上一页
                        </a>
                    </li>
                {% else %}
                    <li class="page-item disabled">
                        <span class="page-link"><span aria-hidden="true">&lsaquo;</span> 上一页</span>
                    </li>
                {% endif %}

                {# 智能页码计算 #}
                {% if paginator.num_pages <= max_visible %}
                    {# 情况1：总页数 <= max_visible - 显示所有页码 #}
                    {% for num in paginator.page_range %}
                        <li class="page-item {% if num == page_obj.number %}active{% endif %}">
                            <a class="page-link" href="?page={{ num }}">{{ num }}</a>
                        </li>
                    {% endfor %}

                {% else %}
                    {# 情况2：总页数 > max_visible - 智能截取页码 #}
                    {% with half=max_visible|divide:2|floor %}
                        {% with start=page_obj.number|subtract:half|max_filter:1 %}
                            {% with end=page_obj.number|add:half|min_filter:paginator.num_pages %}

                                {# 确保显示的页码数始终为max_visible #}
                                {% with page_count=end|subtract:start|add:1 %}
                                    {% if page_count < max_visible %}
                                        {% if start == 1 %}
                                            {% with end=max_visible %}
                                                {% for num in start|range_filter:end %}
                                                    <li class="page-item {% if num == page_obj.number %}active{% endif %}">
                                                        <a class="page-link" href="?page={{ num }}">{{ num }}</a>
                                                    </li>
                                                {% endfor %}
                                            {% endwith %}
                                        {% else %}
                                            {% with start=paginator.num_pages|subtract:max_visible|add:1 %}
                                                {% for num in start|range_filter:paginator.num_pages %}
                                                    <li class="page-item {% if num == page_obj.number %}active{% endif %}">
                                                        <a class="page-link" href="?page={{ num }}">{{ num }}</a>
                                                    </li>
                                                {% endfor %}
                                            {% endwith %}
                                        {% endif %}
                                    {% else %}
                                        {% for num in start|range_filter:end %}
                                            <li class="page-item {% if num == page_obj.number %}active{% endif %}">
                                                <a class="page-link" href="?page={{ num }}">{{ num }}</a>
                                            </li>
                                        {% endfor %}
                                    {% endif %}
                                {% endwith %}

                            {% endwith %}{% endwith %}{% endwith %}
                {% endif %}

                {# 下一页按钮 #}
                {% if page_obj.has_next %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ page_obj.next_page_number }}" title="下一页">
                            下一页 <span aria-hidden="true">&rsaquo;</span>
                        </a>
                    </li>
                {% else %}
                    <li class="page-item disabled">
                        <span class="page-link">下一页 <span aria-hidden="true">&rsaquo;</span></span>
                    </li>
                {% endif %}

                {# 末页按钮 #}
                {% if page_obj.has_next %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ paginator.num_pages }}" title="末页">
                            末页 <span aria-hidden="true">&raquo;</span>
                        </a>
                    </li>
                {% else %}
                    <li class="page-item disabled">
                        <span class="page-link">末页 <span aria-hidden="true">&raquo;</span></span>
                    </li>
                {% endif %}
            </ul>
        </div>

    {% endwith %}{% endwith %}